package com.hjt.service.impl;

import com.hjt.db.pojo.GameScreenRecordDO;
import com.hjt.manager.GameScreenRecordManager;
import com.hjt.service.ClientService;
import com.hjt.service.param.UploadActionRecordParam;
import com.hjt.service.vo.ActionRecordListVO;
import com.hjt.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClientServiceImpl implements ClientService {
    @Resource
    GameScreenRecordManager gameScreenRecordManager;
    @Value("${file.upload.snapshot.save.path}")
    private String fileUploadSnapshotSavePath;
    @Value("${file.upload.snapshot.save.prefix}")
    private String prefix;

    @Override
    public void uploadActionRecord(UploadActionRecordParam param) {
        File file = FileUtil.saveFile(fileUploadSnapshotSavePath, param.getName(), param.getImgData());
        GameScreenRecordDO record = new GameScreenRecordDO();
        record.setDeviceId(param.getDeviceId());
        record.setX(param.getPointX());
        record.setY(param.getPointY());
        record.setCreateTime(new Date());
        String absolutePath = file.getAbsolutePath();
        absolutePath = absolutePath.replace("\\","/").substring(absolutePath.indexOf(prefix) + prefix.length());
        record.setScreenSnapshot(absolutePath);
        gameScreenRecordManager.saveOrUpdate(record);
    }

    @Override
    public ActionRecordListVO actionRecordList() {
        ActionRecordListVO result = new ActionRecordListVO();
        List<ActionRecordListVO.Info> collect = gameScreenRecordManager.getOldTopTen().stream().map(ActionRecordListVO.Info::build).collect(Collectors.toList());
        result.setList(collect);
        return result;
    }
}
